package com.example.hxds.dr.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.example.hxds.common.exception.HxdsException;
import com.example.hxds.common.util.MicroAppUtil;
import com.example.hxds.common.util.PageUtils;
import com.example.hxds.dr.controller.form.LoginForm;
import com.example.hxds.dr.db.dao.DriverDao;
import com.example.hxds.dr.db.dao.DriverSettingsDao;
import com.example.hxds.dr.db.dao.WalletDao;
import com.example.hxds.dr.db.pojo.DriverEntity;
import com.example.hxds.dr.db.pojo.DriverSettingsEntity;
import com.example.hxds.dr.db.pojo.WalletEntity;
import com.example.hxds.dr.service.DriverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DriverServiceImpl implements DriverService {

    @Autowired
    DriverDao driverDao;
    @Autowired
    DriverSettingsDao driverSettingsDao;
    @Autowired
    WalletDao walletDao;
    @Autowired
    MicroAppUtil microAppUtil;

    @Override
    public long hasDriver(Map param) {
        return driverDao.hasDriver(param);
    }
    @Override
    @Transactional
    @LcnTransaction
    public String registerNewDriver(Map param) {
        String code = MapUtil.getStr(param, "code");
        String openId = microAppUtil.getOpenId(code);
        HashMap<String, String> tempParam = new HashMap<String, String>(){
            {put("openId",openId);}
        };
        //查询是否存在已经注册的司机
        long count = driverDao.hasDriver(tempParam);
        if (count!=0){
            throw new HxdsException("该微信无法注册");
        }
        param.put("openId",openId);

        //司机注册
        driverDao.registerNewDriver(param);
        //查询司机id
        String driverId = driverDao.selectDriverId(openId);

        //司机初始化设置
        DriverSettingsEntity driverSettingsEntity=new DriverSettingsEntity();
        driverSettingsEntity.setDriverId(Long.valueOf(driverId));
        JSONObject json=new JSONObject();
        json.set("autoAccept",1);
        json.set("orientation","");
        json.set("listenService",true);
        json.set("orderDistance",0);
        json.set("rangeDistance",5);
        driverSettingsEntity.setSettings(json.toString());
        driverSettingsDao.insertDriverSettings(driverSettingsEntity);

        //司机钱包初始化
        WalletEntity walletEntity = new WalletEntity();
        walletEntity.setDriverId(Long.parseLong(driverId));
        walletEntity.setBalance(new BigDecimal(0));
        walletEntity.setPassword(null);
        walletDao.insert(walletEntity);
        return driverId;
    }

    @Override
    @Transactional
    @LcnTransaction
    public int updateDriverAuth(Map<String, Object> param) {
        return driverDao.updateDriverAuth(param);
    }

    @Override
    public HashMap login(String code) {
        String openId = microAppUtil.getOpenId(code);
        HashMap map=driverDao.login(openId);
        if (map!=null&&map.containsKey("archive")){
            int temp= MapUtil.getChar(map,"archive");
            boolean archive = temp == 1;
            map.replace("archive",archive);
        }
        return map;
    }

    @Override
    public HashMap searchDriverBaseInfo(Long driverId) {
        HashMap map=driverDao.searchDriverBaseInfo(driverId);
        String temSummary = MapUtil.getStr(map, "summary");
        JSONObject summary = JSONUtil.parseObj(temSummary);
        map.replace("summary",summary);
        return map;
    }

    @Override
    public PageUtils searchDriverByPage(Map param) {
        long count = driverDao.searchDriverCount(param);
        List<HashMap>list=null;
        if (count == 0) {
            list=new ArrayList<HashMap>();
        }else {
            list=driverDao.searchDriverByPage(param);
        }
        int start= (Integer) param.get("start");
        int length= (Integer) param.get("length");
        param.put("start",start);
        param.put("length",length);
    return new PageUtils(list, count, start, length);
    }

    @Override
    public HashMap searchDriverAuth(long driverId) {
        return driverDao.searchDriverAuth(driverId);
    }

    @Override
    public HashMap searchDriverRealSummary(long driverId) {
        return driverDao.searchDriverRealSummary(driverId);
    }


    @Override
    @Transactional
    @LcnTransaction
    public int updateDriverRealAuth(Map param) {
        int rows = driverDao.updateDriverAuth(param);
        return rows;
    }

}
